<template>
  <div id="socialSecurityRule">
    <div class="pageHead">
      <el-form ref="ruleForm" :model="ruleForm" class="ruleForm">
        <el-form-item>
          <el-input placeholder="规则名称" v-model="ruleForm.keyWord" />
        </el-form-item>
        <el-form-item>
          <CitySelect
            :data.sync="ruleForm.cityId"
            placeholder="参保城市"
            @change="(data) => (ruleForm.cityId = data.insuredCityId)"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="isConfirmLoading"
            @click="handleSearch"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item style="float: right; text-align: right; margin: 0">
          <el-button type="primary" @click="handleEdit(-1)">新增规则</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="pageMain">
      <div class="tableContent">
        <el-table
          :data="tableData"
          style="width: 100%"
          height="100%"
          v-loading="tableLoading"
          size="medium"
          class="pageInnerTable"
        >
          <el-table-column align="center" type="selection" />
          <el-table-column align="left" label="规则名称" prop="ruleName" />
          <el-table-column align="left" label="参保城市" prop="insuredCity" />
          <el-table-column label="个人社保" prop="personalSS" align="right" />
          <el-table-column label="公司社保" prop="companySS" align="right" />
          <el-table-column label="个人公积金" prop="personalAF" align="right" />
          <el-table-column label="公司公积金" prop="companyAF" align="right" />
          <el-table-column
            label="备注"
            prop="remarks"
            align="left"
            class-name="remarksColumn"
          />
          <el-table-column label="操作" width="150" align="center">
            <div class="btnList" slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope.row.Id)"
                >编辑</el-button
              >
              <el-button type="text" @click="handleEdit(scope.row.Id, 2)"
                >复制</el-button
              >
              <el-button type="text" @click="handleDelete(scope.row.Id)"
                >删除</el-button
              >
            </div>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :small="true"
        background
        @size-change="(data) => handleSearchChange(data, 'pageSize')"
        @current-change="(data) => handleSearchChange(data, 'pageIndex')"
        :current-page="ruleForm.PageIndex"
        :page-sizes="[10, 20, 50]"
        :page-size="ruleForm.PageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        class="pagingChild"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import CitySelect from "./components/CitySelect.vue";
import { math } from "@/util/math.js";
export default {
  name: "socialSecurityRule",
  components: {
    CitySelect,
  },
  data() {
    return {
      ruleForm: {
        cityId: 0,
        keyWord: "",
        pageIndex: 1,
        pageSize: 10,
      },
      isConfirmLoading: false,
      tableData: [],
      tableLoading: false,
      total: 0,
      activeId: -1,
    };
  },
  methods: {
    searchDataList() {
      this.tableLoading = true;
      this.$api.socialSecurity
        .GetPageList(this.ruleForm)
        .then((res) => {
          console.log(res);
          if (!res.data.data) {
            this.tableData = [];
            return this.$message.warning("暂无数据");
          }
          if (res.status == 200) {
            // this.tableData = res.data.data.items;
            this.formatTableData(res.data.data.items);
            this.total = res.data.data.totalCount;
          }
          console.log(this.tableData);
        })
        .finally(() => {
          this.tableLoading = false;
        });
    },
    formatTableData(listData) {
      this.tableData = listData.map((e) => {
        for (const key in e) {
          if (
            ["personalSS", "companySS", "personalAF", "companyAF"].includes(key)
          ) {
            e[key] = math.toFixed(e[key]);
          }
        }
        return e;
      });
    },
    deleteRuleData(ruleId) {
      this.$api.socialSecurity.DeleteSsafRule({ ruleId }).then((res) => {
        if (res.data.status == 200) {
          this.$message.success(res.data.message);
          this.searchDataList();
        } else {
          this.$message.warning(res.data.message);
        }
      });
    },
    handleSearch() {
      this.ruleForm.pageIndex = 1;
      this.searchDataList();
    },
    handleSearchChange(data, prop) {
      this.ruleForm[prop] = data;
      this.searchDataList();
    },
    handleEdit(activeId, operateType = 1) {
      this.activeId = activeId;
      this.$router.push({
        path: "/socialSecurityRuleEdit",
        query: {
          activeId: this.activeId,
          operateType,
        },
      });
    },
    handleDelete(ruleId) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteRuleData(ruleId);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleChange(data) {
      console.log(data);
    },
  },
  activated() {
    this.searchDataList();
  },
};
</script>
<style lang="less" scoped>
@import url("../less/common.less");
#socialSecurityRule {
  .tableContent {
    padding: 0;
  }
  .pagingChild {
    float: right;
    margin-right: 10px;
    margin-top: 5px;
    .el-pagination__total {
      line-height: 28px;
    }
  }
  .ruleForm {
    .el-form-item {
      float: left;
      width: 150px;
      margin-right: 15px;
      .el-input {
        width: 100%;
      }
    }
  }
  .btnList {
    :deep( .el-button) {
      margin: 0;
    }
  }
}
</style>
<style lang="less">
.remarksColumn {
  padding-left: 25px !important;
  // background-color: red;
}
</style>